home *** CD-ROM | disk | FTP | other *** search
/ Night Owl 6 / Night Owl's Shareware - PDSI-006 - Night Owl Corp (1990).iso / 020a / dvpt20.zip / VRTEST.C < prev    next >
C/C++ Source or Header  |  1991-12-12  |  5KB  |  185 lines

  1. /******************************************************************/
  2. /*                                                                */
  3. /*            Digitized Voice Programmer's Toolkit                */
  4. /*            ------------------------------------                */
  5. /*                                                                */
  6. /*                    Recording example                           */
  7. /*                                                                */
  8. /*           Copyright (c) 1991, Farpoint Software                */
  9. /*                                                                */
  10. /******************************************************************/
  11.  
  12. #include "vrmod.h"
  13.  
  14. #include <stdlib.h>
  15. #include <stdio.h>
  16. #include <malloc.h>
  17. #include <fcntl.h>
  18. #include <io.h>
  19. #include <sys\types.h>
  20. #include <sys\stat.h>
  21. #include <bios.h>
  22. #include <string.h>
  23.  
  24. /*-------------------------------------------------------------------*/
  25.  
  26. #define max_buf_size  524288L
  27.  
  28. char logo[] = "Digitized Voice Recording Test Program\r\n"
  29.  "Copyright(c) 1991, Farpoint Software\r\n";
  30.  
  31. char filename[128];        /* file to receive voice data */
  32. char huge *buffer;         /* RAM buffer to hold voice data while recording */
  33. int handle;                /* handle of voice data file */
  34. short comport;             /* number of COM port attached to digitizer */
  35.  
  36. /*-------------------------------------------------------------------*/
  37.  
  38. void main(argc, argv)
  39. int argc;
  40. char ** argv;
  41.  
  42. {
  43. long buf_size, byte_count;
  44. unsigned short sizelo, sizehi;   /* used to partition file save process */
  45. unsigned short i;
  46. char huge *loadptr;              /* 32 bit pointer used during writing */
  47. int delayctr;                    /* internal timing delay from RCALIBRATE */
  48. int calflag;                     /* status word returned from RCALIBRATE */
  49. long retval;
  50.  
  51. /* display the logo */
  52.  
  53. puts(logo);
  54.  
  55. /* make sure there are two arguments on the command line */
  56.  
  57. if ( argc != 3 )
  58.     {
  59.     puts("Command line must include COM port number (1 - 4) and file name.");
  60.     exit(1);
  61.     }
  62.  
  63. /* get the COM port number */
  64.  
  65. argv++;
  66. comport = (short)((**argv) - '0');
  67.  
  68. /* get the file name */
  69.  
  70. argv++;
  71. strcpy(filename, *argv);
  72.  
  73. /* open or create the file, truncating if it already exists */
  74.  
  75. handle = open(filename, O_BINARY|O_RDWR|O_CREAT|O_TRUNC, S_IREAD|S_IWRITE);
  76. if ( handle == -1 )
  77.     {
  78.     puts("Error opening file.");
  79.     exit(1);
  80.     }
  81.  
  82. /* find out how much memory is available */
  83.  
  84. buf_size = GETMEMAVAIL();
  85. if ( buf_size == 0L )
  86.     {
  87.     close(handle);
  88.     puts("No memory available for buffer.");
  89.     exit(1);
  90.     }
  91. if ( buf_size > max_buf_size )
  92.     buf_size = max_buf_size;
  93.  
  94. printf("Available memory = %ld bytes, equivalent to %ld seconds.\n",
  95.     buf_size, buf_size / 16572L);
  96.  
  97. /* allocate memory for the buffer */
  98.  
  99. buffer = (char huge *)halloc(buf_size, 1);
  100. if ( buffer == NULL )
  101.     {
  102.     close(handle);
  103.     puts("Unable to allocate buffer memory.");
  104.     exit(1);
  105.     }
  106.  
  107. /* calibrate for CPU speed */
  108.  
  109. puts("Calibrating...");
  110. retval = RCALIBRATE();
  111. calflag = (int)(retval & 0xFFFF);
  112. delayctr = (int)(retval >> 16);
  113. switch ( calflag )
  114.     {
  115.     case 1:
  116.         close(handle);
  117.         hfree(buffer);
  118.         puts("This CPU is too slow to run this program.");
  119.         exit(1);
  120.     case 2:
  121.         close(handle);
  122.         hfree(buffer);
  123.         puts("This program will not function properly under Enhanced-mode Microsoft Windows.");
  124.         exit(1);
  125.     }
  126. printf("delay counter = %d\n", delayctr);   /* to satisfy your curiosity */
  127.  
  128. /* beep to indicate start of recording */
  129.  
  130. puts("Press any key (or digitizer \"stop\" switch) to end recording.");
  131. puts("\x07Recording...");
  132.  
  133. /* perform the recording */
  134.  
  135. byte_count = RECORDVOICE(buffer, buf_size, comport);
  136.  
  137. /* check the return code for errors */
  138.  
  139. if ( byte_count == -1L )
  140.     {
  141.     close(handle);
  142.     hfree(buffer);
  143.     puts("Invalid COM port.");
  144.     exit(1);
  145.     }
  146.  
  147. /* show the number of bytes recorded */
  148.  
  149. printf("%ld bytes recorded.\n", byte_count);
  150.  
  151. /* write the data to the file */
  152.  
  153. puts("Writing file...");
  154. sizelo = (unsigned short)(byte_count & 0x7FFFL);
  155. sizehi = (unsigned short)(byte_count >> 15);
  156. loadptr = buffer;
  157. for ( i = 0 ; i < sizehi ; i++)
  158.     {
  159.     if ( 32768U != (unsigned)write(handle, loadptr, 32768U) )
  160.         {
  161.         close(handle);
  162.         hfree(buffer);
  163.         puts("File write error.");
  164.         exit(1);
  165.         }
  166.     loadptr += 32768;
  167.     }
  168. if ( sizelo )
  169.     {
  170.     if ( sizelo != (unsigned)write(handle, loadptr, sizelo) )
  171.         {
  172.         close(handle);
  173.         hfree(buffer);
  174.         puts("File write error.");
  175.         exit(1);
  176.         }
  177.     }
  178.  
  179. /* close the file and free the allocated memory */
  180.  
  181. close(handle);
  182. hfree(buffer);
  183. puts("Done.");
  184. }
  185.